package tms.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Destination{

	public static final String TABLE_NAME = "Destination";
	public static String[] COLUMNS = {"DestName", "DestCode", "IsActive"};

	private int id = -1;
	private String destName = "";
	private String destCode = "";
	private boolean isActive = false;

	public String[] getValues() {
		return new String[] {"'" + getDestName() + "'", "'" + getDestCode() + "'",
				isActive() == true ? "1" : "0"};
	}

	static Destination createEntityFrom(Map<String, Object> map) {

		Destination entity = new Destination();

		entity.setId((Integer) map.get("Id".toLowerCase()));
		entity.setDestName((String) map.get("DestName".toLowerCase()));
		entity.setDestCode((String) map.get("DestCode".toLowerCase()));
		entity.setActive((Integer) map.get("IsActive".toLowerCase()) == 1);

		return entity;
	}

	public String toString() {
		return destName + " (" + destCode + ")";
	}

	/***** Following methods is important, please don't modify them *************/

	/**
	 * Updating if obj is an existed object otherwise insert.
	 * @param obj
	 * @return true if success otherwise false
	 */
	public static boolean save(Destination obj) {
		assert (obj != null);
		if (obj.getId() > 0) {
			return update(obj);
		} else {
			return insert(obj);
		}
	}

	/**
	 * Insert given Destination to database.
	 * @param obj
	 * @return
	 */
	private static boolean insert(Destination obj) {
		assert (obj instanceof Destination);
		return DbManager.insert(Destination.TABLE_NAME, Destination.COLUMNS, ((Destination) obj).getValues());
	}

	private static boolean update(Destination obj) {
		assert (obj instanceof Destination);
		return DbManager.update(Destination.TABLE_NAME, Destination.COLUMNS,
				((Destination) obj).getValues(), "id = " + ((Destination) obj).getId());
	}

	public static boolean delete(Destination obj) {
		assert (obj instanceof Destination);
		return DbManager.delete(Destination.TABLE_NAME,
				Destination.TABLE_NAME + ".id = " + ((Destination) obj).getId());
	}

	/**
	 * Get the corresponding Destination to where clause.
	 * @param where condition (ex: Destination.DestName = 'Paris')
	 * @return corresponding Destination or null
	 */
	public static Destination getFirstDynamic(String where) {
		Map<String, Object> map = DbManager.getFirstDynamic(Destination.TABLE_NAME, where);
		return (map != null ? (Destination) Destination.createEntityFrom(map) : null);
	}

	/**
	 * Get a list of Destination corresponding to where clause.
	 * @param where condition (ex: Destination.DestName = 'Paris')
	 * @return corresponding list of Destination or an empty list
	 */
	public static List<Destination> getListDynamic(String where) {
		List<Destination> lsResult = new ArrayList<Destination>();

		List<Map<String, Object>> rs = DbManager.getListDynamic(Destination.TABLE_NAME, where);

		for (Map<String, Object> map : rs)
		{
			lsResult.add(Destination.createEntityFrom(map));
		}

		return lsResult;
	}

	/****************************************************************************/

	@XmlElement
	public int getId() {
		return id;
	}

	private void setId(int id) {
		this.id = id;
	}

	@XmlElement
	public String getDestName() {
		return destName;
	}

	public void setDestName(String destName) {
		this.destName = destName;
	}

	@XmlElement
	public String getDestCode() {
		return destCode;
	}

	public void setDestCode(String destCode) {
		this.destCode = destCode;
	}

	@XmlElement
	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

}
